<template>
  <NModal :show="state.show" @UpdateShow="close">
    <div ref="modalRef" class="list-dia">
      <MacHeader
        :title="state.title"
        :isFullscreen="isFullscreen"
        :showDesc="false"
        @toggle="toggle"
        @close="close"
      />
      <div class="content">
        <NDataTable
          max-height="50vh"
          :loading="tableInfo.listLoading"
          :columns="columns"
          :data="tableInfo.tableData"
          :pagination="false"
        />
        <div class="pagination">
          <NPagination
            v-model:page="tableInfo.queryForm.pageNo"
            :page-count="tableInfo.total"
            :page-sizes="tableInfo.pageSizes"
            show-quick-jumper
            show-size-picker
            @updatePage="pageChange"
            @updatePageSize="sizeChange"
          />
        </div>
      </div>
    </div>
  </NModal>
  <user-detail ref="detailRef" @childer="handChilder" />
  <user-childer ref="childerRef" />
</template>
<script setup lang="ts">
import { ref, reactive, h, defineAsyncComponent } from 'vue'
import type { Ref } from 'vue'
import {
  DataTableColumns,
  NModal,
  NDataTable,
  NPagination,
  NImage
} from 'naive-ui'
import MacHeader from '@/components/codeBlock/MacHeader'
import { useFullscreen } from '@vueuse/core'
import photoListApi from '@/service/api/photo/list'
import { useApi } from '@/hooks/useApi'
const UserDetail = defineAsyncComponent(
  () => import('@/views/operation/user/components/Detail.vue')
)
const UserChilder = defineAsyncComponent(
  () => import('@/views/operation/user/components/UserChilder.vue')
)

const detailRef = ref()
const handUser = (row: any) => {
  detailRef.value.open(row.userid)
}

const childerRef = ref()
const handChilder = (row: any) => {
  childerRef.value.open(row)
}

const { tableInfo, pageChange, sizeChange, queryData } = useApi({
  isRequest: false,
  getData: photoListApi.buyList,
  initForm: {
    photo_id: ''
  }
})

const columns: DataTableColumns<any> = [
  {
    title: '头像',
    key: 'avatar',
    align: 'center',
    render(row: any) {
      return h(NImage, {
        src: row.user.avatar,
        width: '50'
      })
    }
  },
  {
    title: '昵称',
    key: 'avatar',
    align: 'center',
    render(row: any) {
      return h(
        'div',
        {
          class: 'cursor-pointer',
          onClick: () => handUser(row)
        },
        { default: () => row.user.nickname }
      )
    }
  },
  {
    title: '购买日期',
    key: 'create_at'
  }
]

const state = reactive({
  show: false,
  title: ''
})
const modalRef: Ref<any> = ref()
const {
  isFullscreen,
  toggle
}: { isFullscreen: Ref<boolean>; toggle: () => Promise<void> } =
  useFullscreen(modalRef)
const close = () => {
  state.show = false
}
const open = (row: any): void => {
  state.title = row.name
  tableInfo.queryForm.photo_id = row.id
  queryData()
  state.show = true
}

defineExpose({
  open
})
</script>
<style scoped lang="scss">
.list-dia {
  width: 800px;
  background: #fff;
  padding: 10px;
  border-radius: 5px;
  .add-text {
    margin-left: 10px;
  }
  .content {
    margin-top: 20px;
  }
  .pb20 {
    padding-bottom: 20px;
  }
  .pagination {
    margin-top: 10px;
    display: flex;
    justify-content: flex-end;
  }
}
</style>
